package cn.xerllent.common.service.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import cn.xerllent.common.entity.Msg;
import cn.xerllent.common.entity.vo.MsgUserVo;
import cn.xerllent.common.service.MsgService;
import cn.xerllent.common.service.MsgUserService;
import cn.xerllent.common.service.dto.MsgFilter;
import cn.xerllent.common.service.mapper.MsgMapper;

/**
 */
@Service
//@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class MsgServiceImpl extends BaseServiceImpl<MsgMapper, Msg> implements MsgService {

	
	@Autowired
	private MsgUserService msgUserService;
	
	/**
	 * 分页查询
	 * */
    @Override
    public Map<String, Object> queryAll(MsgFilter filter, Pageable pageable) {
    	pageHelper(pageable);
        PageInfo<MsgUserVo> page = new PageInfo<>(baseMapper.queryMyMsgs(filter));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("list", page.getList());
        map.put("total", page.getTotal());
        return map;
    }

	/*
	 * 创建发送消息记录
	 * */
	@Override
	public void msgSend(Msg msg){
		this.save(msg);
		//创建MsgUser记录
		msg.getUsers().stream().forEach(o->{o.setMsgId(msg.getId());});
		msgUserService.saveBatch(msg.getUsers());
		
		
//		Connection conn=null;PreparedStatement pstmt=null;ResultSet res=null;String sql=null;
//		try {
//			conn = DbConnectionManager.getConnection();
//			//创建Msg记录
//			sql="insert into x_msg (sendId,sender,sendtime,content,type) values(?,?,?,?,?)";
//			pstmt = conn.prepareStatement(sql);
//			pstmt.setString(1,msg.sendId);
//			pstmt.setString(2,msg.sender);
//			pstmt.setString(3,msg.sendtime);
//			pstmt.setString(4,msg.content);
//			pstmt.setInt(5,msg.type);
//			pstmt.executeUpdate();
//			pstmt.close();
//			//查出这条Msg记录
//			sql="select id from x_msg where sendId=? and sendtime=?";
//			pstmt = conn.prepareStatement(sql);
//			pstmt.setString(1, msg.sendId);
//			pstmt.setString(2, msg.sendtime);
//			res = pstmt.executeQuery();
//			if(res.next()){
//				msg.id=res.getInt("id");
//			}
//			res.close();pstmt.close();
//
//			//创建MsgUser记录
//			for(MsgUser u:msg.users){
//				u.msgId=msg.id;
//				sql="insert into x_msg_user (msgId,phase,uId,name,recvtime,readmark,delmark) values(?,?,?,?,?,?,?)";
//				pstmt = conn.prepareStatement(sql);
//				pstmt.setLong(1,u.msgId);
//				pstmt.setInt(2,u.phase);
//				pstmt.setString(3,u.uId);
//				pstmt.setString(4,u.name);
//				if(u.recvtime!=null&&u.recvtime.length()>10){
//				pstmt.setString(5,u.recvtime);
//				}else{
//					pstmt.setNull(5, java.sql.Types.TIMESTAMP);
//				}
//				pstmt.setInt(6,u.readmark);
//				pstmt.setInt(7,u.delmark);
//				pstmt.executeUpdate();
//			}
//			return true;
//		}
//		catch (Exception ex) {
//			System.err.println("msgCreate err:"+ex.getMessage());
//			return false;
//		}finally{
//			if(sql!=null)sql=null;
//			if(res!=null)try {res.close();} catch (SQLException e) {}
//			if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
//			if(conn!=null)try {conn.close();} catch (SQLException e) {}
//		}
	}
}
